package com.bank.doman.auth.server;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.bank.common.exception.ParamException;
import com.bank.common.io.PageBo;
import com.bank.common.io.PageVo;
import com.bank.common.util.MySqlUtil;
import com.bank.doman.auth.dao.RoleDao;
import com.bank.doman.auth.dao.RoleGrantDao;
import com.bank.doman.auth.dao.UserRoleDao;
import com.bank.doman.auth.dao.model.Role;
import com.bank.doman.auth.dao.model.RoleGrant;
import com.bank.doman.auth.dao.model.UserRole;
import com.bank.doman.auth.io.bo.*;
import com.bank.doman.auth.io.vo.RoleVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Fu Zhaohui
 * @version 1.0.0
 * @date 2019/8/19 16:51
 */
@Service
public class RoleService {

    @Resource
    private RoleDao roleDao;
    @Resource
    private RoleGrantDao roleGrantDao;
    @Resource
    private UserRoleDao userRoleDao;

    public RoleVo getById(Long id) {
        return BeanUtil.toBean(roleDao.selectByPrimaryKey(id),RoleVo.class);
    }

    public RoleVo create(RoleAddBo bo) {
        ParamException.isNotNull(roleDao.queryByName(bo.getName()),"该角色已存在不要重复添加");
        Role entity = BeanUtil.toBean(bo, Role.class);
        Role role = roleDao.insert(entity);
        return BeanUtil.toBean(role,RoleVo.class);
    }

    public void update(RoleEditBo bo) {
        Role role = roleDao.selectByPrimaryKey(bo.getId());
        ParamException.isNull(role);
        if(!role.getName().equals(bo.getName())){
            ParamException.isNotNull(roleDao.queryByName(bo.getName()),"该角色已存在");
        }
        roleDao.updateByPrimaryKeySelective(BeanUtil.toBean(bo,Role.class));
    }

    public void delete(Long id) {
        roleDao.deleteById(id);
    }

    public PageVo<RoleVo> page(PageBo<RoleQueryBo> pageBo) {
        PageHelper.startPage(pageBo.getPage(),pageBo.getPageSize(), MySqlUtil.orderBy(pageBo.getSort(),pageBo.isAsc()));
        PageInfo<Role> info = new PageInfo(roleDao.queryListRole(pageBo.getParam()));
        List<RoleVo> list = BeanUtil.copyToList(info.getList(),RoleVo.class);
        return new PageVo<>(info.getTotal(),pageBo.getPage(),pageBo.getPageSize(),list);
    }
    @Transactional(rollbackFor = Exception.class)
    public void allotMenu(RoleAllotMenuBo bo){
        roleGrantDao.deleteRoleMenu(bo.getRoleId());
        if(CollectionUtil.isNotEmpty(bo.getMenus())){
            roleGrantDao.addRoleMenu(bo.getMenus(),bo.getRoleId());
        }
    }


    public List<String> getRoleMenu(String roleId){
        List<RoleGrant> roleGrants = roleGrantDao.getRoleMenu(roleId);
        return roleGrants.stream().map(RoleGrant::getGrantId).collect(Collectors.toList());
    }
    @Transactional(rollbackFor = Exception.class)
    public void allotRole(UserAllotRoleBo bo){
        userRoleDao.deleteRoleMenu(bo.getUserId());
        if(CollectionUtil.isNotEmpty(bo.getRoleId())){
            userRoleDao.addRoleMenu(bo.getRoleId(),bo.getUserId());
        }
    }


    public List<String> getUserRole(String userId){
        List<UserRole> roleGrants = userRoleDao.getUserRole(userId);
        return roleGrants.stream().map(UserRole::getRoleId).collect(Collectors.toList());
    }
}
